﻿using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(MyOBJ))]
public class MyOBJInspector : Editor {
	MyOBJ Target;
	string SelectName = "";
	GameObject SelectObj;
	bool AutoSetName = true;
	GameObject auto;
	string console = "";
	void OnEnable(){
		if(Target == null)
			Target = target as MyOBJ;
	}
	public override void OnInspectorGUI(){
			showList();
			if (GUI.changed)
				Undo.RegisterCompleteObjectUndo(Target,"save");	
		}
	void showList(){
			AutoSetName = EditorGUILayout.Toggle("自动设置名字",AutoSetName);
			GUILayout.BeginHorizontal("box");
				if(GUILayout.Button("-",GUILayout.Width(30)))
					SelectName = "";
					SelectName = EditorGUILayout.TextField("搜索名字",SelectName);
			GUILayout.EndHorizontal();		
			GUILayout.BeginHorizontal("box");
				if(GUILayout.Button("-",GUILayout.Width(30)))
					SelectObj = null;	
		SelectObj = EditorGUILayout.ObjectField("搜索物体",SelectObj,typeof(UnityEngine.GameObject),true) as UnityEngine.GameObject;
			GUILayout.EndHorizontal();
		auto = EditorGUILayout.ObjectField("自动添加",auto,typeof(UnityEngine.GameObject),true)as UnityEngine.GameObject;
			EditorGUILayout.LabelField(console);
			if(auto){
				Target.l_ListObj[Target.l_ListObj.Count - 1].obj = auto;
				Target.l_ListObj[Target.l_ListObj.Count - 1].objname = auto.name;
				console = "添加物体>"+auto.name;
				auto = null	;
				
			}
			if(Target.l_ListObj == null || Target.l_ListObj.Count == 0)
				AddNewItem();
			for(var i = 0;i < Target.l_ListObj.Count;i++){	
				bool boolean;
				boolean = CheckShow(Target.l_ListObj[i]);
				if(boolean)
					ShowItem(i,Target.l_ListObj[i]);
			}
			if(Target.l_ListObj[Target.l_ListObj.Count - 1].objname !="")
				AddNewItem();
		}
	bool CheckShow(ObjData target){
			if(SelectName == "" && SelectObj == null )
				return true;
			if(SelectName != "" && !target.objname.Contains(SelectName))
				return false;
			if(SelectObj != null && SelectObj!= target.obj)
				return false;
			return true;
	}
	void ShowItem(int id,ObjData target){
			if(target.obj == null )
				GUI.backgroundColor = Color.red;
			else{
				if(target.objname == "" )	
					GUI.backgroundColor = Color.white;
				else
					GUI.backgroundColor = Color.green;
			}
			GUILayout.BeginHorizontal("box");
				GUI.backgroundColor = Color.white;
				GUILayout.Label("ID:"+id);
		target.obj = EditorGUILayout.ObjectField(target.obj,typeof(GameObject),true) as GameObject;
				EditorGUI.BeginDisabledGroup(target.obj == null);
					if(GUILayout.Button("<"))
						target.obj.name = target.objname;	
					if(GUILayout.Button(">"))
						target.objname = target.obj.name;
				EditorGUI.EndDisabledGroup();
				if(AutoSetName && target.obj && target.objname == ""){
					target.objname = target.obj.name;
				}
				target.objname = EditorGUILayout.TextField(target.objname);
		
				if(GUILayout.Button("-"))
					Target.l_ListObj.RemoveAt(id);
			GUILayout.EndHorizontal();
	}
	void AddNewItem(){
			ObjData obj = new ObjData();
			obj.objname = "";
			Target.l_ListObj.Add(obj);
	}
}